import Qs from 'qs';
import Axios from 'axios'
import baseURL from '_conf/url'
import {Message, LoadingBar} from 'iview'
import Cookies from 'js-cookie'
import {TOKEN_KEY} from '@/libs/util'


Axios.defaults.baseURL = baseURL
//Axios.defaults.withCredentials = true

// request拦截器
Axios.interceptors.request.use(config => {
    LoadingBar.start()
    const token = Cookies.get(TOKEN_KEY)
    if (token) {
        config.headers['x-access-token'] = token // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
}, error => {
    LoadingBar.error()
    // 对请求错误做些什么
    return Promise.reject(error)
})

// respone拦截器
Axios.interceptors.response.use(response => {
    LoadingBar.finish()
    const res = response.data

    if (res.code !== 20000) {
        // 50008登录失效
        if (res.code === 50008) {
            Cookies.remove(TOKEN_KEY)
            window.location.href = '/#/login'
            Message.error(res.message)
        } else if (res.code === 50099) {//50099通用异常
            Message.error(res.message)
        }
    }
    return Promise.resolve(res)
}, error => {
    LoadingBar.error()
    Message.error({
        content: '服务内部错误',
        duration: 3
    })
    // 对响应错误做点什么
    return Promise.reject(error)
})

export const postRequest = (url, params) => {
    return Axios({
        method: 'post',
        url: url,
        data: params,
        transformRequest: function (data) {
            return Qs.stringify(data, {
                arrayFormat: 'repeat'
                // arrayFormat: 'indices'
            });
        },
        // transformRequest: [function (data) {
        //     let ret = ''
        //     for (let it in data) {
        //         if (data[it] != null)
        //             ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        //     }
        //     return ret
        // }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    })
}

export const postJsonRequest = (url, params) => {
    return Axios({
        method: 'post',
        url: url,
        data: params,
        headers: {
            'Content-Type': 'application/json'
        },
    })
}

export const uploadFileRequest = (url, params) => {
    return Axios({
        method: 'post',
        url: url,
        data: params,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
export const getRequest = (url, params) => {
    return Axios({
        method: 'get',
        url: url,
        data: params,
        transformRequest: function (data) {
            return Qs.stringify(data, {
                arrayFormat: 'repeat'
            });
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    })
}
export const deleteRequest = (url, params) => {
    return Axios({
        method: 'delete',
        url: url,
        data: params,
        transformRequest: function (data) {
            return Qs.stringify(data, {
                arrayFormat: 'repeat'
            });
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    })
}
export const postU = (url, params) => {
    return Axios({
        method: 'post',
        url: url,
        data: params
    })
}
